﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Lor;
using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Fether;
using ExpenseSystem.Core.CustomerModules;
using ExpenseSystem.Core.DataAddOn;

namespace ExpenseSystem.Core.OrderModules
{
    public class OrderUpdateCustomerManager : OrderManager
    {
        OrderFetcher fetcher = new OrderFetcher();
        OrderManager orgOrderManager;
        CustomerManager cusManager = new CustomerBaseManager();
        public OrderUpdateCustomerManager(OrderManager orderManager)
        {
            this.orgOrderManager = orderManager;
        }
        private bool UpdateCustomer(long site, long cusId, decimal amount, out string message)
        {
            bool result = false;
            CustomerManager customerManager = CustomerManager.CreateInstance();
            CustomerFetcher customerFetcher = new CustomerFetcher();
            CustomerModel customerInfo = customerFetcher.Get(site, cusId, out message);
            if (customerInfo == null)
            {
                LogHelper.Error(this.GetType().Name,"无法找到对应的客户信息");
                return false;
            }
            customerInfo.Receiveable += amount;
            result = customerManager.Save(customerInfo, out message);
            if(result)
            {
                LogHelper.Info(this.GetType().Name, String.Format("回写客户{0}应收余额失败", customerInfo.Code));

            }
            else
            {
                LogHelper.Info(this.GetType().Name, String.Format("回写客户{0}应收余额成功", customerInfo.Code));
            }
            return result;
        }

        public override bool Approved(long id, out string message)
        {
            OrderModel model;
            decimal amount = 0;
            if (id == 0)
            {
                message = "传入的参数有误";
                return false;
            }
            else
            {
                model = fetcher.Get(profile.SiteId, id, out message);
                if (model == null)
                    return false;
                amount += model.Total;
            }
            bool result = orgOrderManager.Approved(id, out message);
            if (result)
            {
                result = UpdateCustomer(profile.SiteId, model.CustomerId, amount, out message);
            }
            return result;
        }
        public override bool Revert(long id, out string message)
        {
            OrderModel model;
            decimal amount = 0;
            if (id == 0)
            {
                message = "传入的参数有误";
                return false;
            }
            else
            {
                model = fetcher.Get(profile.SiteId, id, out message);
                if (model == null)
                    return false;
                amount -= model.Total;
            }
            bool result = orgOrderManager.Revert(id, out message);
            if (result)
            {
                result = UpdateCustomer(profile.SiteId, model.CustomerId, amount, out message);
            }
            return result;
        }

        public override bool Delete(OrderModel Order, out string message)
        {
            return orgOrderManager.Delete(Order, out message);
        }

        public override bool DeleteById(long[] OrderId, out string message)
        {
            return orgOrderManager.DeleteById(OrderId, out message);
        }

        public override bool Save(OrderModel Order, out string message, params IAddable<OrderModel>[] addables)
        {
            return orgOrderManager.Save(Order, out message, addables);
        }
    }
}
